package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.KfnProjectUser;
import com.ruoyi.system.domain.vo.KfnCustomerVo;
import com.ruoyi.system.domain.vo.KfnProjectUserVo;
import com.ruoyi.system.mapper.KfnCustomerMapper;
import com.ruoyi.system.mapper.KfnProjectUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnProjectBo;
import com.ruoyi.system.domain.vo.KfnProjectVo;
import com.ruoyi.system.domain.KfnProject;
import com.ruoyi.system.mapper.KfnProjectMapper;
import com.ruoyi.system.service.IKfnProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * KFN项目管理Service业务层处理
 *
 * @author Panda
 * @date 2023-12-20
 */
@RequiredArgsConstructor
@Service
public class KfnProjectServiceImpl implements IKfnProjectService {

    private final KfnProjectMapper baseMapper;
    private final KfnCustomerMapper customerMapper;
    private final KfnProjectUserMapper kfnProjectUserMapper;

    /**
     * 查询KFN项目管理
     */
    @Override
    public KfnProjectVo queryById(String projectId){
        KfnProjectVo kfnProjectVo = baseMapper.selectVoById(projectId);

        LambdaQueryWrapper<KfnProjectUser> kfnProjectUserLambdaQueryWrapper = Wrappers.lambdaQuery(KfnProjectUser.class);
        kfnProjectUserLambdaQueryWrapper.eq(KfnProjectUser::getProjectId, projectId);
        List<KfnProjectUserVo> kfnProjectUserVos = kfnProjectUserMapper.selectVoList(kfnProjectUserLambdaQueryWrapper);
        kfnProjectVo.setKfnProjectUserVoList(kfnProjectUserVos);
        return kfnProjectVo;
    }

    /**
     * 查询KFN项目管理列表
     */
    @Override
    public TableDataInfo<KfnProjectVo> queryPageList(KfnProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnProject> lqw = buildQueryWrapper(bo);
        Page<KfnProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询KFN项目管理列表
     */
    @Override
    public List<KfnProjectVo> queryList(KfnProjectBo bo) {
        LambdaQueryWrapper<KfnProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnProject> buildQueryWrapper(KfnProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectNumber()), KfnProject::getProjectNumber, bo.getProjectNumber());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), KfnProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), KfnProject::getProjectType, bo.getProjectType());
        lqw.eq(bo.getProjectPrePrice() != null, KfnProject::getProjectPrePrice, bo.getProjectPrePrice());
        lqw.eq(bo.getProjectPrice() != null, KfnProject::getProjectPrice, bo.getProjectPrice());
        lqw.eq(bo.getProjectStartDate() != null, KfnProject::getProjectStartDate, bo.getProjectStartDate());
        lqw.eq(bo.getProjectEndDate() != null, KfnProject::getProjectEndDate, bo.getProjectEndDate());
        lqw.eq(bo.getProjectPreStartDate() != null, KfnProject::getProjectPreStartDate, bo.getProjectPreStartDate());
        lqw.eq(bo.getProjectPreEndDate() != null, KfnProject::getProjectPreEndDate, bo.getProjectPreEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getThirdSysNumber()), KfnProject::getThirdSysNumber, bo.getThirdSysNumber());
        lqw.eq(bo.getCustomerId() != null, KfnProject::getCustomerId, bo.getCustomerId());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerContacts()), KfnProject::getCustomerContacts, bo.getCustomerContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerContactNumber()), KfnProject::getCustomerContactNumber, bo.getCustomerContactNumber());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerReferences()), KfnProject::getCustomerReferences, bo.getCustomerReferences());
        return lqw;
    }

    /**
     * 新增KFN项目管理
     */
    @Override
    public Boolean insertByBo(KfnProjectBo bo) {
        KfnProject add = BeanUtil.toBean(bo, KfnProject.class);
        validEntityBeforeSave(add);

        // 获取客户信息
        KfnCustomerVo customer = customerMapper.selectVoById(bo.getCustomerId());

        // 设置KfnProject的需要插入的字段
        add.setCustomerName(customer.getCustomerName());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改KFN项目管理
     */
    @Override
    public Boolean updateByBo(KfnProjectBo bo) {
        KfnProject update = BeanUtil.toBean(bo, KfnProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除KFN项目管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
